package com.mySampleApplication.server;

import java.sql.*;

/**
 * Created by Navuhodonosor on 12/11/2014.
 */
public class DatabaseManager {

    private static DatabaseManager instance = new DatabaseManager();

    private DatabaseManager(){

    }

    public static DatabaseManager getInstance(){
        if (instance == null){
            return new DatabaseManager();
        }  else return instance;
    }


    public Connection createConnection() throws SQLException {
        try {
            Class.forName("oracle.jdbc.OracleDriver").newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/orcl","system","admin");
        return connection;
    }

    public void getData(){
        String selectTableSQL = "SELECT * from USERS";
        Connection connection = null;
        Statement statement = null;
        try {
            connection = createConnection();
            statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(selectTableSQL);
            while (rs.next()) {
                String name = rs.getString("NAME");
                String last_name = rs.getString("LAST_NAME");
                String login = rs.getString("LOGIN");
                String password = rs.getString("PASSWORD");
                System.out.println("name : " + name + " last name: " + last_name + " login: " + login + " password: " + password);
            }
            connection.close();
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }

    }

    public boolean add(String name, String lastname, String login, String password){
        String insertSQL = "INSERT INTO USERS" + "(NAME,LAST_NAME,LOGIN,PASSWORD) " + "VALUES" + "('" + name + "','" + lastname + "','" + login + "','" + password +"')";
        Connection connection = null;
        Statement statement = null;
        try {
            connection = createConnection();
            statement = connection.createStatement();
            statement.executeUpdate(insertSQL);
            connection.close();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    public void delete(String name) {
        Connection connection;
        Statement statement;
        try {
            String url = "DELETE USERS WHERE NAME = '" + name + "'";
            connection = createConnection();
            statement = connection.createStatement();
            statement.execute(url);
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void main(String arg[]){
        DatabaseManager.getInstance().getData();
    }

}
